﻿using System;
using System.Collections;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
using Mammoo.WebServer.BLL;
using Mammoo.WebServer.Model;
using Mammoo.Ajax;
using System.Web.SessionState;

namespace web.manage.service
{
    /// <summary>
    /// $codebehindclassname$ 的摘要说明
    /// </summary>
    public class member : IHttpHandler, IRequiresSessionState
    {
        private MemberService service = new MemberService();

        public void ProcessRequest(HttpContext context)
        {
            string op = context.Request["op"];
            AjaxResult result = null;
            try
            {
                switch (op)
                {
                    case "validate":
                        result = Validate(context);
                        break;
                    case "login":
                        result=Login(context);
                        break;
                    case "register":
                        result = Register(context);
                        break;
                }
            }
            catch (Exception e)
            {
                result = new AjaxResult();
                result.success = ReturnStatus.Fail;
                result.msg = e.Message;
            }
            finally
            {
                context.Response.Write(AjaxService.WriteAjax(result));
            }
        }

        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public AjaxResult Login(HttpContext context)
        {
            string username = context.Request["username"];
            string password = context.Request["password"];
            MemberInfo model = service.Login(username, password);
            if (model != null)
            {
                context.Session["member"] = model;
                return new AjaxResult(true, "登录成功！");
            }
            else
            {
                return new AjaxResult(true, "登录失败！");
            }
        }

        /// <summary>
        /// 注册
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public AjaxResult Register(HttpContext context)
        {
            MemberInfo model = (MemberInfo)JsonHelper.Convert2Entity(context.Request["model"], typeof(MemberInfo));

            bool res = service.Register(model);
            string msg = res ? "注册成功！" : "注册失败！";
            return new AjaxResult(true, msg);
        }

        /// <summary>
        /// 验证
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public AjaxResult Validate(HttpContext context)
        {
            string username = context.Request["Member_Username"];
            string email = context.Request["Member_Email"];

            bool res = service.Validate(username, email);
            string msg = res ? "此用户已经存在" : "此用户不存在！";
            return new AjaxResult(res, msg);
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
